//
//  LunBoTu.m
//  ui11lunbotu
//
//  Created by dllo on 15/11/8.
//  Copyright (c) 2015年 liyinkai. All rights reserved.
//

#import "LunBoTu.h"
#import "UIImageView+ShowImage.h"
#import "AdsModel.h"
@implementation LunBoTu
-(void)dealloc {
    [_array release];
    [_title release];
    [super dealloc];
}
-(instancetype)initWithFrame:(CGRect)frame witharray:(NSArray *)array{
    if (self = [super initWithFrame:frame]) {
        _array = [[NSMutableArray alloc] initWithArray:array];
        [self createScrollView];
        self.backgroundColor = [UIColor whiteColor];
        UIPageControl *page = [[UIPageControl alloc] initWithFrame:CGRectMake(self.bounds.size.width - 100, self.frame.size.height - 15, 10 + (_array.count - 2) * 15, 10)];
        page.numberOfPages = _array.count - 2;
        page.tag = 1000;
        page.tintColor = [UIColor redColor];
        page.backgroundColor = [UIColor blackColor];
        [page addTarget:self action:@selector(page:) forControlEvents:UIControlEventValueChanged];
        [self addSubview:page];
    }
    return self;
}

-(void)createScrollView {
    UIScrollView *scroll = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.bounds.size.width, self.bounds.size.height - 20)];
    scroll.pagingEnabled = YES;
    scroll.tag = 1001;
    if (self.array.count) {
        [_array insertObject:[_array lastObject] atIndex:0];
        [_array addObject:_array[1]];
        for (int i = 0; i < _array.count; i++) {
            UIImageView *image = [[UIImageView alloc] initWithFrame:CGRectMake(self.frame.size.width * i, 0, self.frame.size.width, self.frame.size.height - 20)];
            AdsModel *model = _array[i];
            [image WXZM:model.imgsrc];
//            保持图片的比例不变
            image.contentMode = UIViewContentModeScaleAspectFill;
            image.clipsToBounds = YES;
            [scroll addSubview:image];
            [image release];
        }
    }
    scroll.delegate = self;
    scroll.contentOffset = CGPointMake(self.frame.size.width, 0);
    scroll.contentSize = CGSizeMake(self.bounds.size.width * _array.count, 0);
    [self addSubview:scroll];
    _title = [[UILabel alloc] initWithFrame:CGRectMake(0, self.frame.size.height - 20, self.bounds.size.width - 60, 20)];
    _title.font = [UIFont systemFontOfSize:12];
    AdsModel *model2 = _array[1];
    _title.text = model2.title;
    [self addSubview:self.title];
    [_title release];
    [scroll release];
//    NSTimer *time = [NSTimer scheduledTimerWithTimeInterval:3 target:self selector:@selector(timer) userInfo:nil repeats:YES];
}
-(void)page:(UIPageControl *)sender {
    UIScrollView *view = (UIScrollView *)[self viewWithTag:1001];
    [view setContentOffset:CGPointMake(view.frame.size.width * sender.currentPage, 0) animated:YES];
}
-(void)timer {
    
    UIScrollView *scroll = (UIScrollView *)[self viewWithTag:1001];
    [scroll setContentOffset:CGPointMake(scroll.contentOffset.x + self.frame.size.width, 0) animated:YES];
    if (scroll.contentOffset.x == 0) {
        [scroll setContentOffset:CGPointMake(self.frame.size.width * (_array.count - 2), 0)];
    }
    if (scroll.contentOffset.x == self.frame.size.width * (_array.count - 1)) {
        [scroll setContentOffset:CGPointMake(self.frame.size.width , 0)];
    }
    int a = scroll.contentOffset.x / self.frame.size.width + 1;
    AdsModel *model = _array[a];
    _title.text = model.title;
}
-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
        UIPageControl *page = (UIPageControl *)[self viewWithTag:1000];
        page.currentPage = scrollView.contentOffset.x / scrollView.frame.size.width - 1;
    if (scrollView.contentOffset.x == 0) {
        [scrollView setContentOffset:CGPointMake(self.frame.size.width * (_array.count - 2), 0)];
        page.currentPage = _array.count - 3;
    }
    if (scrollView.contentOffset.x == self.frame.size.width * (_array.count - 1)) {
        [scrollView setContentOffset:CGPointMake(self.frame.size.width , 0)];
        page.currentPage = 0;
    }
    
    int a = scrollView.contentOffset.x / self.frame.size.width;
    AdsModel *model = _array[a];
    _title.text = model.title;

//

    
}


@end
